<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Model;

use Hyperf\Utils\Arr;
use Hyperf\DbConnection\Db;

/**
 * @property int $id
 * @property string $name
 * @property int $status
 * @property string $desc
 */
class Role extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'role';

    public $timestamps = false;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'status' => 'integer'];

    /**
     * 获取角色拥有的访问菜单列表.
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function access()
    {
        return $this->hasOne(RoleAccessMenu::class, 'role_id', 'id');
    }

    /**
     * 根据搜索条件获取角色列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getRoleByWhere($where=[], $field="*", $limit=10)
    {
        if(is_array($where)){
            $res =  $this->where($where)->select($field=='*'?'*':explode(',',$field))->paginate(intval($limit));
            if(!empty($res)){
                return for_paginator_data_formater($res);
            }
        }else{
            $res =  $this->whereRaw($where)->select($field=='*'?'*':explode(',',$field))->paginate(intval($limit));
            if(!empty($res)){
                return for_paginator_data_formater($res);
            }
        }
    }

    /**
     * 根据搜索条件获取所有的角色数量
     * @param $where
     */
    public function getAllRole($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 插入角色信息
     * @param $param
     */
    public function insertRole($param)
    {
        try{

            $result =  $this->insertGetId($param);
            if(false === $result){
                return false;
            }else{
                return $result;
            }
        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }

    /**
     * 编辑角色信息
     * @param $param
     */
    public function editRole($id,$param)
    {
        try{
            $result = $this->where('id', $id)->update($param);
            return $result;
        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }

    /**
     * 根据角色id获取角色信息
     * @param $id
     */
    public function getOneRole($id)
    {
        $info = $this->where('id', $id)->first();
        if(empty($info)) return false;

        if(!empty($info)){
            return $info->toArray();
        }
    }

    /**
     * 删除角色
     * @param $id
     */
    public function delRole($id)
    {
        try{

            return $this->where('id', $id)->update(['status'=>0]);

        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }

    // 获取所有的角色信息
    public function getRole()
    {
        return $this->select()->get();
    }

    // 获取角色的权限节点
    public function getRuleById($id)
    {
        return $this->where('id', $id)->value('rule');

    }

    /**
     * 分配权限
     * @param $param
     */
    public function editAccess($param)
    {
        try{
            return $this->where('id' , $param['id'])->update($param);
        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }

    /**
     * 获取角色信息
     * @param $id
     */
    public function getRoleInfo($id,Node $nodeModel)
    {
        $result = $this->where('id', $id)->first()->toArray();
        // 超级管理员权限是 *
        if(empty($result['rule'])){
            $result['action'] = '';
            return $result;
        }else if('*' == $result['rule']){
            $where = '';
        }else{
            $where = 'id in(' . $result['rule'] . ')';
        }

        // 查询权限节点
        $res = $nodeModel->getActions($where);

        foreach($res as $key=>$vo){

            if('#' != $vo['action_name']){
                $result['action'][] = $vo['control_name'] . '/' . $vo['action_name'];
            }
        }

        return $result;
    }
}
